package com.ruoyi.tzai.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.tzai.mapper.UserFavoriteMapper;
import com.ruoyi.tzai.domain.UserFavorite;
import com.ruoyi.tzai.service.IUserFavoriteService;
import com.ruoyi.common.utils.DateUtils;

/**
 * 用户收藏Service业务层处理
 * 
 * @author tzai
 * @date 2025-10-27
 */
@Service
public class UserFavoriteServiceImpl implements IUserFavoriteService 
{
    @Autowired
    private UserFavoriteMapper userFavoriteMapper;

    /**
     * 查询用户收藏
     * 
     * @param favoriteId 用户收藏主键
     * @return 用户收藏
     */
    @Override
    public UserFavorite selectUserFavoriteByFavoriteId(Long favoriteId)
    {
        return userFavoriteMapper.selectUserFavoriteByFavoriteId(favoriteId);
    }

    /**
     * 根据用户ID和文章ID查询用户收藏
     * 
     * @param userId 用户ID
     * @param articleId 文章ID
     * @return 用户收藏
     */
    @Override
    public UserFavorite selectUserFavoriteByUserIdAndArticleId(Long userId, Long articleId)
    {
        return userFavoriteMapper.selectUserFavoriteByUserIdAndArticleId(userId, articleId);
    }

    /**
     * 查询用户收藏列表
     * 
     * @param userFavorite 用户收藏
     * @return 用户收藏
     */
    @Override
    public List<UserFavorite> selectUserFavoriteList(UserFavorite userFavorite)
    {
        return userFavoriteMapper.selectUserFavoriteList(userFavorite);
    }

    /**
     * 新增用户收藏
     * 
     * @param userFavorite 用户收藏
     * @return 结果
     */
    @Override
    @Transactional
    public int insertUserFavorite(UserFavorite userFavorite)
    {
        userFavorite.setCreateTime(DateUtils.getNowDate());
        return userFavoriteMapper.insertUserFavorite(userFavorite);
    }

    /**
     * 批量删除用户收藏
     * 
     * @param favoriteIds 需要删除的用户收藏主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteUserFavoriteByFavoriteIds(Long[] favoriteIds)
    {
        return userFavoriteMapper.deleteUserFavoriteByFavoriteIds(favoriteIds);
    }

    /**
     * 删除用户收藏信息
     * 
     * @param favoriteId 用户收藏主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteUserFavoriteByFavoriteId(Long favoriteId)
    {
        return userFavoriteMapper.deleteUserFavoriteByFavoriteId(favoriteId);
    }

    /**
     * 根据用户ID和文章ID删除用户收藏
     * 
     * @param userId 用户ID
     * @param articleId 文章ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteUserFavoriteByUserIdAndArticleId(Long userId, Long articleId)
    {
        return userFavoriteMapper.deleteUserFavoriteByUserIdAndArticleId(userId, articleId);
    }

    /**
     * 切换收藏状态（收藏/取消收藏）
     * 
     * @param userId 用户ID
     * @param articleId 文章ID
     * @return 结果（true=已收藏，false=已取消）
     */
    @Override
    @Transactional
    public boolean toggleFavorite(Long userId, Long articleId)
    {
        // 查询是否已收藏
        UserFavorite existing = userFavoriteMapper.selectUserFavoriteByUserIdAndArticleId(userId, articleId);
        
        if (existing != null) {
            // 已收藏，则取消收藏
            userFavoriteMapper.deleteUserFavoriteByUserIdAndArticleId(userId, articleId);
            return false;
        } else {
            // 未收藏，则添加收藏
            UserFavorite favorite = new UserFavorite();
            favorite.setUserId(userId);
            favorite.setArticleId(articleId);
            favorite.setCreateTime(DateUtils.getNowDate());
            userFavoriteMapper.insertUserFavorite(favorite);
            return true;
        }
    }
}

